package com.bq.gametrade.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.HomeTradingBuy;
import com.bq.gametrade.utils.FormatUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * @author Feel on 2016/11/29 11:05
 */

public class TradingBuyAdapter extends BaseAdapter {
    private ArrayList<HomeTradingBuy> items;
    private Context context;

    public TradingBuyAdapter(Context context, ArrayList<HomeTradingBuy> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_item_userbuy,null);
            viewHolder.tradingTime = (TextView) convertView.findViewById(R.id.tv_trading_time);
            viewHolder.tradingCount = (TextView) convertView.findViewById(R.id.tv_trading_account);
            viewHolder.tradingMoney = (TextView) convertView.findViewById(R.id.tv_trading_money);
            viewHolder.tradingUnitPrice = (TextView) convertView.findViewById(R.id.tv_trading_unitPrice);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        HomeTradingBuy buy = items.get(position);
        viewHolder.tradingTime.setText(buy.stime);
        viewHolder.tradingCount.setText(buy.cardCount);

        viewHolder.tradingMoney.setText(FormatUtil.valueFormat(buy.price.replace(",",""))+"元");
        viewHolder.tradingUnitPrice.setText(FormatUtil.valueFormatWithTwo(String.valueOf(Double.parseDouble(buy.price.replace(",",""))/Double.parseDouble(buy.cardCount.replace(",","")))));

       return convertView;
    }

    class ViewHolder{
        TextView tradingTime;
        TextView tradingCount;
        TextView tradingMoney;
        TextView tradingUnitPrice;
    }

}
